import {createSlice} from "@reduxjs/toolkit";

/** 创建store步骤 */

const countStore = createSlice({
    name: "counter",
    /** 初始化 store state数据 */
    initialState: {
        count: 0,
    },
    /** 修改状态的函数 */
    reducers:{
        increment: (state, action) => {
            state.count += action.payload;
        },
        decrement: (state, action) => {
            if(state.count !==0){
                state.count -= action.payload;
            }
        }
    }
})

/** 解构store 里面的  action 函数*/

const {increment,decrement} = countStore.actions

/** 获取store 里面的reducer */

const reducer = countStore.reducer

/** 按需导出action */
export {increment,decrement}

/** 默认导出reduce */
export default reducer

/**
 * 引入createSlice
 * 创建createSlice({})
 * initialState 初始化store
 * reducers 创建action
 * 解构 action
 * 获取reducer
 * 导出 action
 * 导出 reducer
 * */